package com.hd.core.project.module.controller;

import java.util.Arrays;

import java.util.List;
import com.hd.core.project.module.base.WrapperBaseController;
import com.hd.core.project.module.config.TeacherModuleProperties;
import com.hd.core.project.module.domain.Student;
import com.hd.core.project.module.service.ClassRosterService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hd.core.framework.aspectj.lang.annotation.Log;
import com.hd.core.framework.aspectj.lang.constant.BusinessType;
import com.hd.core.framework.web.page.TableDataInfo;
import com.hd.core.framework.web.domain.AjaxResult;

/**
 * <p>班级花名册 <p/>
 * <p>注意该模块功能并没有对应的数据库表，而是使用学生管理的表<p/>
 * <p>当前模块只是对应的教师处理自己对应的班级的学生<p/>
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年9月6日
 * @package com.hd.core.project.module.controller
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Controller
@RequestMapping("/module/teacher/class-roster")
public class ClassRosterController extends WrapperBaseController<TeacherModuleProperties> {
	
	@Autowired
	private ClassRosterService classRosterService;
	
	ClassRosterController() {
		super("/class-roster");
	}
	
	/**
	 * <p>跳转到班级花名册视图</p>
	 * <p>访问权限: module-teacher:class-roster:view</p>
	 * <p>路径: /templates/module/teacher/class-roster/class-roster.html</p>
	 * 
	 * @return 班级花名册视图
	 */
	@GetMapping
	@RequiresPermissions("module-teacher:class-roster:view")
	public String studentList(ModelMap mmap){
	    return getResponseViewPath("/class-roster");
	}
	
	/**
	 * <p>查询班计划花名册,只返回当前用户所对应的班级的学生<p/>
	 * <p>根据Entity实体内的成员属性的值来当做查询条件<p/>
	 * <p>访问权限: module-teacher:class-roster:list</p>
	 * 
	 * @param entity 条件实体
	 * @return 符合条件的学生数据
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("module-teacher:class-roster:list")
	public TableDataInfo list(Student entity){
		startPage();
		List<Student> result = classRosterService.selectList(entity, getUserId());
		return getDataTable(result);
	}
	
	/**
	 * <p>跳转到新增花名册学生的视图</p>
	 * <p>路径: /templates/module/teacher/class-roster/add.html</p>
	 * 
	 * @return 班级花名册新增功能视图
	 */
	@GetMapping("/add")
	public String add(){
	    return getResponseViewPath("/add");
	}
	
	/**
	 * <p>新增班级学生<p/>
	 * <p>新增到当前用户所对应的班级下<p/>
	 * <p>访问权限: module-teacher:class-roster:add</p>
	 * 
	 * @param entity 数据实体
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/add")
	@Log(title = "班计花名册", action = BusinessType.INSERT)
	@RequiresPermissions("module-teacher:class-roster:add")
	public AjaxResult addSave(Student entity){
		return toAjax(classRosterService.insert(entity,getUserId()));
	}

	/**
	 * <p>跳转到修改功能视图</p>
	 * <p>根据URL上的id值,先查询出要来,回现到页面上</p>
	 * <p>路径: /templates/module/teacher/class-roster/edit.html</p>
	 * 
	 * @return 修改视图路径
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap){
		Student student = classRosterService.selectById(id);
		mmap.put("student", student);
	    return getResponseViewPath("/edit");
	}
	
	/**
	 * <p>修改并保存<p/>
	 * <p>根据实体的Id值来进行保存操作<p/>
	 * <p>访问权限: module-teacher:class-roster:edit</p>
	 * 
	 * @param entity 参数实体也是判断条件
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping("/edit")
	@RequiresPermissions("module-teacher:class-roster:edit")
	@Log(title = "班计花名册", action = BusinessType.UPDATE)
	public AjaxResult editSave(Student entity){	
		return toAjax(classRosterService.updateById(entity));
	}
	
	/**
	 * <p>根据id删除对应的数据,可以接收多个ID<p/>
	 * <p>访问权限: module-teacher:class-roster:remove</p>
	 * 
	 * @param ids 需要被删除的文明宿舍所对应的id号
	 * @return http状态消息
	 */
	@ResponseBody
	@PostMapping( "/remove")
	@RequiresPermissions("module-teacher:class-roster:remove")
	@Log(title = "班计花名册", action = BusinessType.DELETE)
	public AjaxResult remove(String[] ids){
		List<String> list = Arrays.asList(ids);
		return toAjax(classRosterService.deleteStudents(list));
	}
}
